IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_CreateEntityProperty]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_CreateEntityProperty]
GO
create procedure [dbo].[cms_sp_CreateEntityProperty]
@MetaFieldName nvarchar(250),
@MetaFieldType nvarchar(400),
@EntityTypeParameter nvarchar(400)
as
declare @MetaFieldId int
insert into [dbo].[EntityMetaFields](
[MetaFieldName],
[IsSystem],
[MetaFieldType]
)
values(
@MetaFieldName,
0,
@MetaFieldType
)
set @MetaFieldId = SCOPE_IDENTITY()
declare @maxMetaFieldOrder int
select @maxMetaFieldOrder = MetaFieldOrder from dbo.EntityMetaFieldLinks
where EntityTypeParameter = @EntityTypeParameter
insert into [dbo].[EntityMetaFieldLinks](
[EntityMetaFieldId],
[EntityTypeParameter],
[IsAllowMultipleValues],
[IsRequired],
[MetaFieldOrder]
)
values(
@MetaFieldId,
@EntityTypeParameter,
0,
0,
isnull(@maxMetaFieldOrder, 0) + 1
)
select
	EntityMetaFields.EntityMetaFieldId,
	EntityMetaFieldLinks.EntityMetaFieldLinkId,
	EntityMetaFields.MetaFieldName,
	EntityMetaFields.MetaFieldDescription,
	EntityMetaFields.IsSystem,
	EntityMetaFields.MetaFieldType
from dbo.EntityMetaFields
inner join dbo.EntityMetaFieldLinks
	on EntityMetaFieldLinks.EntityMetaFieldId = EntityMetaFields.EntityMetaFieldId
where EntityMetaFields.EntityMetaFieldId = @MetaFieldId


GO

